package com.chinasoft.mall.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.chinasoft.mall.R;
import com.chinasoft.mall.activity.adapter.SyGroupBuyListAdapter;
import com.chinasoft.mall.framework.BeanCache;
import com.chinasoft.mall.framework.Config;
import com.chinasoft.mall.framework.ControllerCenter;
import com.chinasoft.mall.model.BaseBean;
import com.chinasoft.mall.model.response.GoodsGrpBean;
import com.chinasoft.mall.widget.LoadListView;
import com.chinasoft.mall.widget.LoadListView.OnRefreshListener;

/**
 * 团购
 * @author Administrator
 */
public class SyGroupBuyListActivity extends BaseActivity implements OnRefreshListener{
	
	private LoadListView modellist;
	private BaseBean goodsgrp_bb;
	private SyGroupBuyListAdapter mlAdapter;
	private List<GoodsGrpBean> ggbs=new ArrayList<GoodsGrpBean>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sy_more_modellist2);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initRequest();
	}

	private void initRequest() {
		goodsgrp_bb = BeanCache.getInstance().getResponsdata().get(Config.GET_GOODSGRP);
		if (goodsgrp_bb != null) {
			goodsgrp_bb.reqData.put("pageNo", "1");
			goodsgrp_bb.reqData.put("pageSize", "10");
			success(goodsgrp_bb);
		} else {
			goodsgrp_bb = new BaseBean(Config.GET_GOODSGRP);
			goodsgrp_bb.reqData.put("pageNo", "1");
			goodsgrp_bb.reqData.put("pageSize", "10");
			control.sendMessage(goodsgrp_bb);
		}
	}

	private void initView() {
		modellist=(LoadListView) findViewById(R.id.modellist);
		mlAdapter=new SyGroupBuyListAdapter((MainActivity)this.getParent(), null);
		modellist.setAdapter(mlAdapter);
		modellist.setonRefreshListener(this);
		modellist.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void success(BaseBean obj) {
		super.success(obj);
		if(obj.equals(goodsgrp_bb)){
			ggbs.addAll((List<GoodsGrpBean>)obj.getResData().getData());
			mlAdapter.setData(ggbs);
			modellist.onRefreshComplete();
		}
	}

	@Override
	public void onRefresh() {
		goodsgrp_bb.reqData.put("pageNo", (Integer.parseInt(goodsgrp_bb.reqData.get("pageNo"))+1)+"");
		control.postMessage(goodsgrp_bb);
	}

}
